/**
 * licenses
 *
 */
package com.openjawtech.xscraper.flights.dao.impl;

import com.openjawtech.xscraper.flights.dao.AirportDAO;
import com.openjawtech.xscraper.flights.domain.Airport;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>implement all airport dao method
 *
 * <p>16/02/2009
 * @author Wenbo.Sun
 */
public class AirportDAOImpl extends BaseDAO implements AirportDAO {

    private static final Log log = LogFactory.getLog(AirportDAOImpl.class);

    @Override
    public void delete(Airport airport) {
        log.debug("deleting the airport instance -- id: " + airport.getAirportId());
        try {
            getSession().delete(airport);
            log.debug("airport instance has been deleted!");
        } catch (RuntimeException re) {
            log.error("delete airport instance failed -- id: " + airport.getAirportId(), re);
            throw re;
        }
    }

    @Override
    public List<String> getAllNames() {
        log.debug("retrieving all airport names");
        try {
            String query = "select distinct airport.airportName from Airport airport order by airport.airportName";
            List<String> names = getSession().createQuery(query).list();
            log.debug("all names have been retrieved!");
            return names;
        } catch (RuntimeException re) {
            log.error("retrieve all names failed", re);
            throw re;
        }
    }

    @Override
    public List<String> getArrivals(String name) {
        log.debug("retrieve all arrival names of departure: " + name);
        try {
            String query = "select distinct flight.airportByArrivalId.airportName from Flight flight "
                    + "where flight.airportByDepartureId.airportName = ? order by flight.airportByArrivalId.airportName";
            List<String> names = getSession().createQuery(query).setParameter(0, name).list();
            log.debug("arrival names have been retrieved");
            return names;
        } catch (RuntimeException re) {
            log.debug("retrieve all arrival names failed -- departure: " + name, re);
            throw re;
        }
    }

    @Override
    public Airport getByName(String name) {
        log.debug("retrieve airport by name: " + name);
        try {
            String query = "from Airport airport where airport.airportName = ?";
            Airport airport = (Airport) getSession().createQuery(query).setParameter(0, name).iterate().next();
            log.debug("airport has been retrieved");
            return airport;
        } catch (RuntimeException re) {
            log.error("retrieve airport failed -- name " + name, re);
            throw re;
        }
    }

    @Override
    public boolean isExisted(String name) {
        log.debug("check the existence of airport name: " + name);
        try {
            String query = "from Airport airport where airport.airportName = ?";
            boolean result = getSession().createQuery(query).setParameter(0, name).iterate().hasNext();
            log.debug("check the name succeed");
            return result;
        } catch (RuntimeException re) {
            log.error("check the existence failed -- name " + name, re);
            throw re;
        }
    }

    @Override
    public void save(Airport airport) {
        log.debug("saving the airport instance -- name: " + airport.getAirportName());
        try {
            getSession().save(airport);
            log.debug("airport instance has been saved!");
        } catch (RuntimeException re) {
            log.error("save airport instance failed -- name: " + airport.getAirportName(), re);
            throw re;
        }
    }

    @Override
    public void update(Airport airport) {
        log.debug("updating the airport instance -- id: " + airport.getAirportId());
        try {
            getSession().update(airport);
            log.debug("airport has been updated");
        } catch (RuntimeException re) {
            log.error("update airport failed -- id: " + airport.getAirportId());
            throw re;
        }
    }
}
